package com.itboye.shopcar_adapter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;

import com.itboye.core.object.BaseFragment;
import com.itboye.mengwu.R;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;

public abstract class BaseOrderFragment extends BaseFragment implements OnItemClickListener{

	private XListView xlistview;
	private int pageNum = 1;
 

	private String status;
	
	private BaseAdapter adapter;
	private RelativeLayout gonerela;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		
		return inflater.inflate(R.layout.fragment_allfragment, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		status = onGetStatus();
		if(adapter==null){
//			showProgressDialog("数据加载中，请稍后", true);
		}
		xlistview.setPullRefreshEnable(true);
		xlistview.setPullLoadEnable(true);
		xlistview.setOnItemClickListener(this);
		xlistview.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
//				pullDown();
			}

			@Override
			public void onLoadMore() {
//				pullUp();
			}

		});
		// adapter = new ShopCarAdapter(getApplication(), dataList, this);
		// xlistview.setAdapter(adapter);
		adapter=onGetAdapter();
		xlistview.setAdapter(adapter);
//		pullDown();
		
		

	}

	protected abstract String onGetStatus();

	protected abstract BaseAdapter onGetAdapter();
	
	protected abstract void onPullDownFinish(BaseAdapter adapter,XListView xListView,OrderBean orderBean);

	protected abstract void onPullUpFinish(BaseAdapter adapter,XListView xListView,OrderBean orderBean);
	
	protected abstract void onPullDownError(BaseAdapter adapter,XListView xListView,Exception exception, int code, String msg);

	protected abstract void onPullUpError(BaseAdapter adapter,XListView xListView,Exception exception, int code, String msg);
	

	/*private void pullDown() {

		pageNum = 1;
		MyJsonRequest.Builder<OrderBean> builder = new MyJsonRequest.Builder<OrderBean>()
				.param("uid",(String)SPUtils.get(getContext(), null, SPContants.USER_ID	, "")).apiVer("100")
				.param("curpage", "" + pageNum).param("pagesize", "10").param("status", status)
				.typeKey("BY_Orders_query")
				.requestListener(new XRequestListener<OrderBean>() {

					@Override
					public void onResponse(OrderBean orderBean) {
						
						
							gonerela.setVisibility(View.GONE);
					
						
						
						//((ViewGroup)xlistview.getParent()).addView(gonerela);
						//
						if(orderBean==null)
						{
							Log.v("orderBean------", "null");
						}else
						{
						  Log.v("orderBean------", orderBean.toString());
						
						}
						
						DebugLog.v("orderbean", "haha"+"-----"+status);
						onPullDownFinish(adapter, xlistview, orderBean);
						pageNum++;
						xlistview.stopRefresh();
						try {
							
//							closeProgressDialog();
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}).errorListener(new XErrorListener() {

					@Override
					
					public void onErrorResponse(Exception exception, int code, String msg) {
						
						gonerela.setVisibility(View.VISIBLE);
						xlistview.setEmptyView(gonerela);
					//	Toast.makeText(App.ctx, "haha", 0).show();
						xlistview.stopRefresh();
						onPullDownError(adapter, xlistview, exception, code, msg);
						try {
							
							closeProgressDialog();
						} catch (Exception e) {
							// TODO: handle exception
						}

					}
				});

		MyJsonRequest<OrderBean> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}*/

	/*private void pullUp() {
		MyJsonRequest.Builder<OrderBean> builder = new MyJsonRequest.Builder<OrderBean>()
				.param("uid", (String) SPUtils.get(App.ctx, null, SPContants.USER_ID, "")).apiVer("100")
				.typeKey("BY_Orders_query")
				.param("curpage", "" + pageNum).param("pagesize", "10").param("status", status)

				.requestListener(new XRequestListener<OrderBean>() {

					@Override
					public void onResponse(OrderBean orderBean) {
						xlistview.stopLoadMore();
						pageNum++;
						onPullUpFinish(adapter, xlistview, orderBean);
						
						
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						xlistview.stopLoadMore();
						onPullUpError(adapter, xlistview, exception, code, msg);
					}
				});

		MyJsonRequest<OrderBean> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}
*/
}
